package com.cipe.cmrs.model.ref;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cipe.cmrs.model.Role;

@Entity
@Table(name = "link")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Link extends BaseRef {

	private static final long serialVersionUID = -1562855410333158675L;
	
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "roleprivileges", joinColumns = { @JoinColumn(name = "link") }, inverseJoinColumns = @JoinColumn(name = "roleid"))
    private Set<Role> roles;    
    
	public Set<Role> getRoles() {
		return roles;
	}	

	@Column(name="url")
	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
    public boolean equals(Object object) {
        Link link = (Link) object;
        return url.equals(link.getUrl());
    }
	
    @Override
    public String toString() {
        return url;
    }
    
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(url).toHashCode();
    }
}
